<template>
  <div style="width: 100%; height: 100vh; overflow: hidden;background-image: linear-gradient(to top, #fbc2eb 0%, #a6c1ee 100%);">
    <div style="width: 400px; margin: 150px auto;">
      <div style="color: #EAECF1; font-size: 30px; text-align: center; padding: 30px 0">欢迎登录</div>
      <el-form ref="form" :model="form" size="normal" :rules="rules">
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="请输入账号">
            <template #prefix>
              <el-icon><Avatar /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" show-password placeholder="请输入密码">
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <div style="display: flex">
            <el-input v-model="form.validCode" style="width: 250px;" placeholder="请输入验证码">
              <template #prefix>
                <el-icon><Key /></el-icon>
              </template>
            </el-input>
            <validCode @input="createvalidCode" style="background-color:rgba(255,255,255,0.6);margin-left: 50px" />
          </div>
        </el-form-item>
        <el-form-item>
          <el-row :gutter="50">
            <el-col :span="12">
              <el-button style="width: 175px" type="primary" @click="login">登 录</el-button>
            </el-col>
            <el-col :span="12">
              <el-button style="width: 175px" type="primary" @click="register">注 册</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </div>

  </div>
</template>


<script>
import { Avatar, Lock, Key} from '@element-plus/icons-vue'
import request from "../../utils/request";
import validCode from "@/components/ValidCode"


export default {
  name: "Login",
  components: {
    Avatar,
    Lock,
    Key,
    validCode
  },
  data() {
    return {
      form: {},
      rules:{
        username: [
          { required: true, message: '请输入用户名!', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码!', trigger: 'blur' }
        ],
      },
      validCode: ''
    }
  },
  created() {
    sessionStorage.removeItem("user")
  },
  methods: {
    createvalidCode(data) {
      this.validCode = data
    },
    register() {
      this.$router.push("/register")
    },
    login() {
      this.$refs['form'].validate((valid) => {
        if(valid) {
          if(!this.form.validCode) {
            this.$message.error("请填写验证码")
            return
          }
          if(this.form.validCode.toLowerCase() !== this.validCode.toLowerCase()) {
            this.$message.error("验证码错误")
            return
          }
          request.post("/api/user/login",this.form).then(res => {
            if(res.code === '0') {
              this.$message({
                type: "success",
                message: "登陆成功"
              })
              sessionStorage.setItem("user", JSON.stringify(res.data))  // 缓存用户信息
              if( res.data.role == '1') {
                // 登录成功的时候更新当前路由
                this.$router.push("/")    //登陆成功后实现页面的跳转，跳转到主页
              }
              else if( res.data.role == '2') {
                // 登录成功的时候更新当前路由
                this.$router.push("/index")    //登陆成功后实现页面的跳转，跳转到主页
            }
              }
             else {
              this.$message({
                type: "error",
                message: res.msg
              })
            }
          })
        }
      })

    }
  }
}
</script>

<style scoped>

</style>